package top.zackyoung.bbs.filter;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: ZackYoung
 * @time: 2020/6/14
 */
public class LoginFilter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {

        Object user = req.getSession().getAttribute("userList");
        String url = req.getRequestURI();
        url = url.replace("top/zackyoung/bbs/", "");
        String loginPage = req.getContextPath() + "/login" + "?url=" + url;
        if (user == null) {
            resp.sendRedirect(loginPage);
            return false;
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("================= login filter ru  after ===============");
    }

}
